import java.util.Scanner;

public class C_1100 {
    static int[][] dp;
    static int[] time,value;
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int t = sc.nextInt(),n = sc.nextInt();
        time = new int[n];
        value = new int[n];
        dp = new int[t+1][n+1];
        for (int i = 0; i < dp.length; i++) {
            for (int i1 = 0; i1 < dp[0].length; i1++) {
                dp[i][i1] = -1;
            }
        }
        for (int i = 0; i < n; i++) {
            time[i] = sc.nextInt();
            value[i] = sc.nextInt();
        }
        System.out.println(dfs(t,n,0));
    }
    static int dfs(int t,int n,int nn){
        if (nn==n)
            return 0;
        if (dp[t][nn]!=-1)
            return dp[t][nn];
        int d1,d2 = Integer.MIN_VALUE;
        d1 = dfs(t,n,nn+1);
        if (time[nn]<=t)
            d2 = dfs(t-time[nn],n,nn+1)+value[nn];
        dp[t][nn] = Math.max(d1,d2);
        return dp[t][nn];
    }
}
